package com.h52mm.mybatis.demo.mapper;

import com.h52mm.mybatis.demo.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserZjMapper {

    @Insert("insert into t_user(id,username) values(#{id},#{userName})")
    public void addUser(User user);

    @Update("update t_user set username=#{userName} where id=#{id} ")
    public void updateUser(User user);

    @Delete("delete from t_user where id=#{id}")
    public void deleteUserById(Integer id);

    @Select("<script> select id,username userName from t_user <where> <if test=\"id!=null\"> and id=#{id} </if> <if test=\"userName!=null\"> and username=#{userName} </if></where> </script>")
    public List<User> selectUsers(User user);

    @Select("select id,username userName from t_user where id=#{id}")
    public User selectUserById(Integer id);

    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "userName",column = "userName"),
            @Result(property = "orders" ,column = "id",many = @Many(select="com.h52mm.mybatis.demo.mapper.OrderZjMapper.selectOrdersByUid"))
    })
    @Select("select id,username userName from t_user ")
    public List<User> selectUserOrders();

    @Select("select u.id id,u.username userName from t_user u LEFT JOIN t_user_role o on u.id=o.uid where o.rid=#{rid}")
    public List<User> selectUsersByRid(Integer rid);

}
